package it.kion.util.ui.umenumanager2.preferences;

import it.kion.util.ui.umenumanager2.UMenuManager;
import it.kion.util.ui.umenumanager2.views.MenuManagerView;

import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.preference.FieldEditorPreferencePage;
import org.eclipse.jface.preference.FileFieldEditor;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;

public class UMenuManagerPreferencePage extends FieldEditorPreferencePage implements IWorkbenchPreferencePage {

	static public final String PREF_I18N = "it.kion.util.ui.umenumanager2.pathToi18n";
	static public final String PREF_PATH_TO_EJB = "it.kion.util.ui.umenumanager2.pathToEJB";
	static public final String PREF_PATH_TO_DESKTOP_XML = "it.kion.util.ui.umenumanager2.pathToSiaXX-menu-folder";
	
	public UMenuManagerPreferencePage() {
		super(1);
		setPreferenceStore(UMenuManager.getDefault().getPreferenceStore());
		String bundleVer = UMenuManager.getDefault().getBundle().getHeaders().get("Bundle-Version").toString();
		setTitle("U-GOV Menu Manager (ver. "+bundleVer+")");
		setDescription("Menu Manager");
		initializeDefaults();
	}

	public static void initializeDefaults() {
		IPreferenceStore store = UMenuManager.getDefault().getPreferenceStore();
		String wsLocation = Platform.getLocation().toString();
		String path = store.getString(PREF_PATH_TO_DESKTOP_XML);
		if(path==null) {
			path = wsLocation + MenuManagerView.SIAPR_WEB_NEW_PATH;
		}
		store.setDefault(PREF_PATH_TO_DESKTOP_XML, path);
	}

	public void createFieldEditors() {
		addField(new FileFieldEditor(PREF_PATH_TO_DESKTOP_XML, "File (desktop.xml):", getFieldEditorParent()));
//		addField(new PathEditor(PREF_I18N, "Directory contenenti i file i18n.xml per il menu:", "Scegliere la directory contenente il file i18n.xml", getFieldEditorParent()));
	}

	@Override
	public boolean performOk() {
		boolean res = super.performOk();
		IPreferenceStore store = UMenuManager.getDefault().getPreferenceStore();
		store.setValue(PREF_PATH_TO_DESKTOP_XML, store.getString(PREF_PATH_TO_DESKTOP_XML));
		return res;
	}
	
	public void init(IWorkbench iworkbench) {}

}